package com.cjl.web.backend;


import com.cjl.entity.EbOrder;
import com.cjl.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller//标记为web服务员
@RequestMapping("/order")//工号
public class OrderController {
@Autowired
    private OrderService orderService;

    @RequestMapping("/list")
    public String list(Model model,@RequestParam(value = "page",defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize",defaultValue = "3") int pageSize) {
    System.out.println("web-----receive ");
      final List<EbOrder> orderList= orderService.listByPage(psize,pageSize);
        final Integer totalPage = orderService.findTotalPage(pageSize);
        model.addAttribute("orderList",orderList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute(  "currPage",psize);

        return "manage/order";
    }
    @RequestMapping("/to_edit")
    public String toEdit(Model model, int eoId) {
        System.out.println("点击修改跳过来了，eoId=" + eoId);
        EbOrder ebOrder = orderService.selectOne(eoId);
        model.addAttribute("ebOrder", ebOrder);
        return "manage/order-modify";
    }
    @RequestMapping ("/edit")
    public String edit( EbOrder ebOrder) {
        System.out.println("ebOrder==>" + ebOrder);
        Integer result = orderService.updateOrder(ebOrder);
        if (result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping( "/delete")
    public String delete(Model model, Integer id) {
        //1，参数校验
//2,执行删除（真实项目中delete from 表名where id=?)
        int result = orderService.deleteByPk(id);
        model.addAttribute("result", result);
        if(result == 0) {
//TOD0 要么return错误页面要么跑出异常
        }
        return "manage/manage-result";

    }
}
